237. 删除链表中的节点
为保证权益,题目请参考 237. 删除链表中的节点(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
void deleteNode(ListNode *node)
{
node->val = node->next->val;
while (node->next->next != nullptr)
{
node = node->next;
node->val = node->next->val;
}
node->next = nullptr;
}
};
int main()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Python
python
# 237. 删除链表中的节点
# https://leetcode-cn.com/problems/delete-node-in-a-linked-list/
################################################################################
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
targetNode = node.next
node.val = targetNode.val
node.next = targetNode.next
del targetNode
################################################################################
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29